package com.cipe.cmrs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.bean.ProjectReportBean;
import com.cipe.cmrs.model.Cipeproject;
import com.cipe.cmrs.model.Projectreport;

@Service
@Transactional
public class ProjectReportService extends BaseService<Projectreport>{
	@Autowired
	private CipeprojectService cipeprojectService;
	
	public List<Projectreport> findallProjectReport(String  procedure) {
		return this.dao.getResultListWithNativeQuery(procedure, Projectreport.class);
	}
	
	@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
	public List<Projectreport> findProjectReportsByProjectId(Integer cipeprojectId) {
		List<Projectreport> projectReports = this.dao.getNamedQueryResultList("findProjectReportsByProjectId", 
				new Integer[] { cipeprojectId }, new String[] { "projectId" });
		return projectReports;
	}
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public void persistProjectReportData(ProjectReportBean data) {
		if(data != null) {
			Cipeproject cipeproject = cipeprojectService.findById(data.getCipeprojectId());
			
			// save narrative report lines
			if(data.isNarrativeSectionEditable() == true) {
				List<Projectreport> narrativeReports = data.getNarrativeReports();
				if(narrativeReports != null) {
					for(Projectreport report : narrativeReports) {
						if(!report.isEmpty()) saveOrUpdateReportLine(report, cipeproject);
					}
				}
			}	
		
			// save financial report lines 
			if(data.isFinancialSectionEditable() == true) {
				List<Projectreport> financialReports = data.getFinancialReports();
				if(financialReports != null) {
					for(Projectreport report : financialReports) {
						if(!report.isEmpty()) saveOrUpdateReportLine(report, cipeproject);
					}
				}
			}
			
		}
	}
	
	@Transactional(propagation=Propagation.MANDATORY, readOnly=false)
	private void saveOrUpdateReportLine(Projectreport report, Cipeproject cipeproject) {
		if(report != null) {
			report.setCipeproject(cipeproject);  // reset the correct parent project (just in case..) 

			if(report.getId() == null || report.getId() == 0) {
				this.dao.persist(report);
			} else {
				Projectreport tReport = dao.findById(Projectreport.class, report.getId());
				
				tReport.setReportperiod1(report.getReportperiod1());
				tReport.setReportperiod2(report.getReportperiod2());
				tReport.setReportdue(report.getReportdue());
				tReport.setReportreceived(report.getReportreceived());
				tReport.setFinalStr(report.getFinalStr());
				tReport.setReporttogrants(report.getReporttogrants());
				tReport.setReceivedingrants(report.getReceivedingrants());
				tReport.setApprovedbyfinance(report.getApprovedbyfinance());
				
				this.dao.merge(tReport);
			}
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public void deleteReportLineById(Integer reportId) {
		Projectreport report = this.dao.findById(Projectreport.class, reportId);
		
		if(report != null) {
			this.dao.delete(report);
		}
	}	
	
}
